<?php
// +----------------------------------------------------------------------
// | B5LaravelCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace App\Services;

use App\Cache\MapplyCache;
use App\Helpers\Util\ValidateApi;
use App\Models\Mapply;
use App\Validates\MapplyValidate;
use Illuminate\Support\Str;

/**
 * 微信预约报名
 * Class MapplyService
 * @package App\Services
 */
class MapplyService extends BaseService
{
    public function __construct(bool $loadValidate = true)
    {
        $this->setModel(new Mapply());
        $loadValidate && $this->setValidate(new MapplyValidate());
    }

    public function extField($key=null,$all=false){
        $arr=[
            'name'  =>  ['title'=>'姓名','status'=>1],
            'phone' =>  ['title'=>'联系电话','status'=>1],
            'idcard'=>  ['title'=>'身份证号','status'=>1],
            'sex'   =>  ['title'=>'性别','status'=>1],
            'birthday'=>['title'=>'出生日期','status'=>1]
        ];
        if(!is_null($key)) return isset($arr[$key])?$arr[$key]['title']:'';
        $reArr=[];
        foreach ($arr as $key=>$value){
            if(!$all && !$value['status']) continue;
            $reArr[$key]=$value['title'];
        }
        return $reArr;
    }

    public function checkExtField($key,$val,$title,$require=0){
        $val=trim($val);
        $extField=$this->extField();
        if(!array_key_exists($key,$extField)) return message($key.':'.$title.'参数错误',false);
        if($require && !$val)  return message($title.'不能为空',false);
        if($val){
            switch ($key){
                case 'name':
                    if(!ValidateApi::isChinese($val)) return message($title.'必须为汉字',false);
                    if(!ValidateApi::checkLen($val,'2,4')) return message($title.'长度为2-4',false);
                    break;
                case 'phone':
                    if(!ValidateApi::is_mobile_phone($val)) return message($title.'格式不正确',false);
                    break;
                case 'idcard':
                    if(!ValidateApi::isIdcard($val)) return message($title.'格式不正确',false);
                    break;
                case 'birthday':
                    if(!ValidateApi::isDate($val)) return message($title.'格式不正确',false);
                    break;
                case 'sex':
                    if($val!='男' && $val!='女') return message($title.'错误',false);
                    break;
            }
        }
        return message('',true);
    }

    public function info($id, bool $isArray = true)
    {
        $info= parent::info($id, $isArray);
        if($info){
            $info['banner']=$info['banner']?explode(',',$info['banner']):[];
            $info['regfield']=$info['regfield']?json_decode($info['regfield'],true):[];
        }
        return $info;
    }

    public function after_edit($data)
    {
        $data['id'] && $this->delcache($data['id'],false);
        return parent::after_edit($data); // TODO: Change the autogenerated stub
    }

    public function after_drop($data,$field)
    {
        $this->delcache($data['ids'],false);
        return parent::after_drop($data,$field); // TODO: Change the autogenerated stub
    }

    public function getList($all = false)
    {
        return parent::getList($all, [], [['id', 'desc']]); // TODO: Change the autogenerated stub
    }

    /**
     * 清除缓存
     * @param mixed $ids
     * @param boolean $re
     * @return array
     */
    public function delcache($ids=null,bool $re=true){
        MapplyCache::clear($ids);
        if($re){
            return message('清理缓存完成', true);
        }
    }
}
